package com.wbs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wbs.mapper.TbSpecificationMapper;
import com.wbs.model.TbSpecification;
import com.wbs.model.TbSpecificationExample;
import com.wbs.resutil.PageBean;
import com.wbs.resutil.PageResult;
import com.wbs.service.SpecificationOptionService;
import com.wbs.service.SpecificationService;
import com.wbs.vo.SpecificationOptionVo;
import com.wbs.vo.SpecificationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 规格服务实现类
 * 提供规格相关的业务操作实现
 */
@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private SpecificationOptionService specificationOptionService;

    /**
     * 分页查询规格信息
     * @param pageBean 分页参数
     * @return 分页查询结果
     */
    @Override
    public PageResult<SpecificationVO> findSpecificationPage(PageBean<SpecificationVO> pageBean) {
        TbSpecificationExample specificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = specificationExample.createCriteria();

        // 设置模糊查询条件，当名称不为空时
        if (!StrUtil.isEmpty(pageBean.getBean().getSpecName())){
            criteria.andSpecNameLike("%" + pageBean.getBean().getSpecName() + "%");
        }

        // 开启分页查询
        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<TbSpecification> tbSpecificationList = tbSpecificationMapper.selectByExample(specificationExample);
        PageInfo<TbSpecification> pageInfo = new PageInfo<>(tbSpecificationList);

        // 将TbSpecification转换为SpecificationVO
        List<SpecificationVO> specificationVOList = tbSpecificationList.stream().map(this::convertToSpecificationVO).collect(Collectors.toList());
        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), specificationVOList);
    }

    /**
     * 通过主键查询具体规格
     * @param id 规格ID
     * @return 规格视图对象
     */
    @Override
    public SpecificationVO selectByKey(Long id) {
        TbSpecification specification = tbSpecificationMapper.selectByPrimaryKey(id);
        SpecificationVO specificationVO = convertToSpecificationVO(specification);

        List<SpecificationOptionVo> specificationOptionVoList = specificationOptionService.selectByExample(id);
        specificationVO.setList(specificationOptionVoList);

        return specificationVO;
    }

    /**
     * 更新规格信息
     * @param specificationVO 规格视图对象
     */
    @Override
    public void update(SpecificationVO specificationVO) {
        tbSpecificationMapper.updateByPrimaryKeySelective(new TbSpecification(specificationVO.getId(), specificationVO.getSpecName()));

        specificationOptionService.deleteBySpecId(specificationVO.getId());
        specificationOptionService.addList(specificationVO.getList());
    }

    /**
     * 添加新的规格
     * @param specificationVO 规格视图对象
     */
    @Override
    public void add(SpecificationVO specificationVO) {
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        tbSpecificationExample.createCriteria().andSpecNameEqualTo(specificationVO.getSpecName());
        List<TbSpecification> tbSpecificationList = tbSpecificationMapper.selectByExample(tbSpecificationExample);

        if (tbSpecificationList.isEmpty()){
            tbSpecificationMapper.insertSelective(new TbSpecification(null, specificationVO.getSpecName()));

            TbSpecificationExample newExample = new TbSpecificationExample();
            newExample.createCriteria().andSpecNameEqualTo(specificationVO.getSpecName());
            TbSpecification newSpecification = tbSpecificationMapper.selectByExample(newExample).get(0);

            specificationVO.getList().forEach(x -> x.setSpecId(newSpecification.getId()));
            specificationOptionService.addList(specificationVO.getList());
        }
    }

    /**
     * 删除指定的规格
     * @param id 规格ID
     */
    @Override
    public void delete(Long id) {
        tbSpecificationMapper.deleteByPrimaryKey(id);
        specificationOptionService.deleteBySpecId(id);
    }

    /**
     * 查询所有规格
     * @return 所有规格的列表
     */
    @Override
    public List<SpecificationVO> findSpecificationAll() {
        List<TbSpecification> tbSpecificationList = tbSpecificationMapper.selectByExample(null);
        return tbSpecificationList.stream().map(this::convertToSpecificationVO).collect(Collectors.toList());
    }

    /**
     * 将TbSpecification对象转换为SpecificationVO对象。
     * @param tbSpecification TbSpecification对象
     * @return 转换后的SpecificationVO对象
     */
    private SpecificationVO convertToSpecificationVO(TbSpecification tbSpecification) {
        SpecificationVO specificationVO = new SpecificationVO();
        BeanUtils.copyProperties(tbSpecification, specificationVO);
        return specificationVO;
    }
}
